package com.stb.llh.service;

import com.alibaba.fastjson.JSON;
import com.stb.llh.dao.*;
import com.stb.llh.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Lee
 * @Date 2021/11/7
 * @Description
 */

@Transactional
@Service
public class AdminService {

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private EmotionDao emotionDao;

    @Autowired
    private LifeDao lifeDao;

    public Admin doLogin(String aname,String pwd){
        return adminDao.findAdminByAnameAndPwd(aname,pwd);
    }


    public List<User> getUserList(){
        List<User> all = userDao.findAll();
        return all;
    }

    public User updateUser(User user){
        return  userDao.save(user);
    }

    public boolean delUser(int id) {
        try {
            userDao.deleteById(id);
            return true;
        } catch (Exception e){
            return false;
        }
    }

    public List<Emotion> getEmotionList(){
        return emotionDao.findAll();
    }

    public boolean delEmo(int id){
        try {
            emotionDao.deleteById(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public List<PreLife> getPreLifeList(){
        return lifeDao.findAll();
    }

    public boolean delLife(int id){
        try {
            lifeDao.deleteById(id);
            return false;
        }catch (Exception e){
            return false;
        }
    }


    //大屏


    /**
     * 注册人数占比
     * @return
     */
    public List<Integer> getUserCount(){

        List<Integer> res = new ArrayList<>();
        int i = userDao.countAllByUsernameIsNotNull();
        int j = userDao.countUserNow();
        res.add(i);
        res.add(j);
        return res;

    }

    /**
     * 注册性别占比
     * @return
     */
    public List<Integer> getSexCount(){
        List<Integer> res = new ArrayList<>();
        int i = userDao.maleCount();
        int j = userDao.femaleCount();
        res.add(i);
        res.add(j);
        return res;
    }


    /**
     * 地址占比
     */

    public List<String> getAddressCount(){
        List<String> res = new ArrayList<>();
        int n = 0;
        for (AddressCount addressCount : userDao.getAddressCount()) {
            HashMap<String,Object> map = new HashMap<>();
            map.put("name",addressCount.getAddress()!=null?addressCount.getAddress():"未知");
            map.put("value",addressCount.getCounts()+"");
            res.add(JSON.toJSONString(map));

            if(n++>=6){
                break;
            }
        }

        return res;

    }

    /**
     * 对于吐槽进行分析
     */

    public List<Integer> getEmotionCount(){
        List<Integer> res = new ArrayList<>();
        int i = emotionDao.countAll();
        int j = emotionDao.countEmotionNow();
        int k = emotionDao.countEmotionYesterday();
        res.add(i);
        res.add(j);
        res.add(k);

        return res;
    }
}
